#include <iostream>
#include <fstream>
#include "config.h"
#include "corefuncs.h"
#include "syncfuncs.h"

using namespace std;

void showHelp();

int main( int argc, char *argv[] )
{

	cout << endl << "--------------------------------SyncSoft 0.8-----------------------------------" << endl;
	cout << "This program conceived and developed by Jacob Gardner." << endl;
	cout << "You may freely use and distribute this in its original form." << endl;
	cout << "Run with --help to display help file. " << endl << endl;

	// If no client is provided, find it from config files.
	bool manualClient = false;

    preInitialize();
        
	if( argc > 1 )
	{
		for( int i = 1; i < argc; i++ )
		{
			if( !strcmp( argv[i], "-h" ) )
			{                
				strcpy( hostDir, argv[++i] );
				continue;
			}
			if( !strcmp( argv[i], "-c" ) )
			{
				strcpy( clientDir, argv[++i] );
				manualClient = true;
				continue;
			}         
			if( !strcmp( argv[i], "--force-new-config" ) )
			{
				forceConfig = true;
				continue;
			}
			if( !strcmp( argv[i], "-rmd" ) )
			{
				strcpy( relativeMusicDir, argv[++i] );
				continue;
			}
			if( !strcmp( argv[i], "--help" ) )
			{
				showHelp();
				return 0;
			}
			if( !strcmp( argv[i], "-p" ) )
			{
				i++;
				ignorePlaylistType = PL_M3U;
				if( !strcmp( argv[i], "1" ) )
				{
					ignorePlaylistType = PL_PLS;
				}
			}
			
		}
	}
	
	if( !doesHostExist(hostDir) || forceConfig )
	{
		cout << "Host Config Files Not Found" << endl;
		if( !manualClient )
		{
			cout << "Cannot Create Them Without Client Directory" << endl;
			cout << "Please run again with -c switch." << endl;
			cout << "Exiting..." << endl;
			return 0;
		}

		cout << "Creating Them..." << endl;
		// If our host doesn't exist, create it.
		if( !createHost( hostDir ) )
			return 0;
	}	
	
	if( !loadConfig(hostDir) )
	{
		cout << "Could Not Open Config File in Host Directory" << endl;
		cout << "Make sure that it exists and try again. " << endl << endl;	
		return 0;
	}
	
	if( !directoryExists( clientDir ) )
	{
		cout << "Device not connected or Path is Incorrect." << endl;
		cout << "Please connect your device or change to the appropriate path and try again." << endl << endl;
		return 0;
	}

	cout << "Initializing Sync: " << endl;
	cout << "DO NOT REMOVE/UNMOUNT YOUR DEVICE DURING SYNC PROCESS!" << endl;
	
	initializeSync();
	cout << "Beginning Sync Process: " << endl;
	startSync(hostDir);
	cout << "Syncing complete!" << endl;
	cout << "You may now unmount your device!" << endl;
	
	cout << endl;
	
	return 0;
}

void showHelp()
{
	// Console Commands: 
	// -h hostDir       	Directory of the Host
	// -c clientDir     	Directory of the Client
	// --force-new-config	Forcibly create a new config file
	// *-rmd relMusicDir	Relative root directory on Client for Music
	// -p					Playlist Type
	// --help				Show This
	// * Not Yet Implemented
	cout << "syncSoft [OPTIONS]" << endl << endl;
	cout << "SyncSoft Switches: " << endl;
	cout << " -h hostDir\t\tDirectory of the host" << endl;
	cout << " -c clientDir\t\tDirectory of the client" << endl;
	cout << " -p playlistType\tType of Playlist (0 - M3U, 1 - PLS)" << endl;
	cout << " -rmd relMusicDir\tRelative root directory on client for music." << endl;
	cout << "--force-new-config\tForcibly create a new config file" << endl;	
	cout << "--help\t\t\tShow this help file." << endl << endl;
	cout << "SyncSoft Help Notes: " << endl;
	cout << "If running this program for the first time or in a new directory, you must" << endl;
	cout << "use the -h, -c, and -rmd switches for the config file to be created correctly." << endl << endl;
	cout << "If you have incorrectly created the config file or paths/devices have changed," << endl;
	cout << "you may create a new one using the --force-new-config switch." << endl << endl;
	cout << "The ignore playlist functionality is not fully working at the current time." << endl;
	cout << "If you wish to use it, you must create a M3U file with plain directories" << endl;
	cout << "pointing to the offending files. One per line." << endl;
	cout << "I believe rythmbox creates M3U playlists in this fastion." << endl << endl;
	cout << "Full functionality coming later. " << endl << endl;
	
}

